#include "smallBlock.h"

//Main Constructor
SmallBlock::SmallBlock( int newX, int newY, short newMatrix[9] )
{
    x = newX ;
    y = newY ;

    int index = 0 ;
    for( int i=0 ; i<3 ; ++i ) {
        for( int j=0 ; j<3 ; ++j ) {
            matrix[ i ][ j ].setValue( newMatrix[ index++ ] ) ;
        }
    }
}



std::ostream& operator<<( std::ostream& out, SmallBlock block )
{
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            out << block.getElement( i, j ) << ' ' ;
        }
        out << std::endl ;
    }
    return out ;
}

std::ostream& SmallBlock::print( std::ostream& out)
{
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            out << *getCell( i, j ) << std::endl ;
        }
    }
    return out ;
}

//Check rules
//True all is good
bool SmallBlock::checkRules()
{
    bool array[] = {true,   false, false, false,
                            false, false, false,
                            false, false, false} ;
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            int value = matrix[ i ][ j ].getValue() ;
            array[ value ] = true ;
        }
    }
    for( int index=0 ; index<10 ; ++index )
        if( false == array[ index ] )
            return false ;
    return true ;
}

bool SmallBlock::allIsFilled()
{
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            if( 0 == getCell( i, j )->getValue() )
            {
                return false ;
            }
        }
    }
    return true ;
}

bool SmallBlock::assumptionsIs()
{
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            if( true == getCell( i, j )->assumptionsIs() )
            {
                return true ;
            }
        }
    }
    return false ;
}


//-----Getters ans setters-----

//Get X coord of this small block
int SmallBlock::getX()
{
    return x ;
}

//Get Y coord of this small block
int SmallBlock::getY()
{
    return y ;
}

//Get busy count of busy elements
int SmallBlock::getBusyElements()
{
    int counter = 0 ;
    for( int i=0 ; i<3 ; ++i )
    {
        for( int j=0 ; j<3 ; ++j )
        {
            if( 0 != matrix[ i ][ j ].getValue() )
                ++counter ;
        }
    }
    return counter ;
}


Cell* SmallBlock::getCell( const int& currX, const int& currY )
{
    return &matrix[ currX ][ currY ] ;
}

short SmallBlock::getElement( const int& currX, const int& currY )
{
    return matrix[ currX ][ currY ].getValue() ;
}
